/**
 * 
 */
package com.agilegroup.crm.requests;

import static com.agilegroup.crm.utils.CONSTANTS.ADD_APPOINTMENT_POST;

import java.util.Collections;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.agilegroup.crm.model.Appointment;
import com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest;


/**
 * @author hisham
 *
 */
public class NewAppointmentJsonRequest extends SpringAndroidSpiceRequest<Appointment>{

	private Appointment appointment;
	
	public NewAppointmentJsonRequest(Appointment appointment) {
		super(Appointment.class);
		this.appointment=appointment;
	}
	
	@Override
	public Appointment loadDataFromNetwork() throws Exception {
		HttpHeaders requestHeaders = new HttpHeaders();
	    requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
	    HttpEntity<?> requestEntity = new HttpEntity<Object>(appointment,requestHeaders);
	    //RestTemplate restTemplate = getRestTemplate();
	    RestTemplate restTemplate = new RestTemplate();
	    restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
	    ResponseEntity<Appointment> responseEntity = restTemplate.exchange(ADD_APPOINTMENT_POST, HttpMethod.POST, requestEntity, Appointment.class);
	    Appointment appointment = responseEntity.getBody();
		return appointment;
	}
}